package com.jhecis.common;

import com.jhecis.dto.ScriptContent;
import org.springframework.stereotype.Component;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**所有的核心在这
 * Js脚本执行类
 */
@Component
public class JsEngine {

    /**
     * 执行js脚本
     * @param content
     */
    public  String execJs(ScriptContent content){
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine engine = scriptEngineManager.getEngineByName("JavaScript");
        engine.put("register", new RegisterFunction());
        //register.RestSharpPost
        engine.put("input",content.Data);
        try {
            String jsCode= content.JavaScriptCode +" "+content.FunctionName+"(input,register);";
            System.out.println(jsCode);
            System.out.println("执行完成！");
            return (String)engine.eval(jsCode);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return "";
    }
}
